local tostr = {}

function tostr.tostringex(value)
  if type(value)=='table' then
    return tostr.table2str(value)
  elseif type(value)=='string' then
    return "\'"..value.."\'"
  else
    return tostring(value)
  end
end

function tostr.table2str(t)
  if t == nil then return "" end
  local retstr= "{"

  local i = 1
  for key,value in pairs(t) do
    local signal = ","
    if i==1 then
      signal = ""
    end

    if key == i then
      retstr = retstr..signal..tostr.tostringex(value)
    else
      if type(key)=='number' or type(key) == 'string' then
        retstr = retstr..signal..'['..tostr.tostringex(key).."]="..tostr.tostringex(value)
      else
        if type(key)=='userdata' then
          retstr = retstr..signal.."*s"..tostr.tostringex(getmetatable(key)).."*e".."="..tostr.tostringex(value)
        else
          retstr = retstr..signal..key.."="..tostr.tostringex(value)
        end
      end
    end

    i = i+1
  end

   retstr = retstr.."}"
   return retstr
end

return function(value)
  local text = tostr.tostringex(value)
  tostr = nil
  collectgarbage()
  return text
end